package com.ruoyi.basicData.categoryManagement.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.basicData.client.mapper.ClientMapper;
import com.ruoyi.basicData.supplier.mapper.SupplierMapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.CategoryManagement;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.categoryManagement.mapper.CategoryManagementMapper;
import com.ruoyi.basicData.categoryManagement.service.ICategoryManagementService;

/**
 * 分类管理Service业务层处理
 * 
 * @author Yuan
 * @date 2024-05-03
 */
@Service
@RequiredArgsConstructor
public class CategoryManagementServiceImpl implements ICategoryManagementService
{
    private final CategoryManagementMapper categoryManagementMapper;

    /**
     * 查询分类管理
     * 
     * @param categoryId 分类管理主键
     * @return 分类管理
     */
    @Override
    public CategoryManagement selectCategoryManagementByCategoryId(Long categoryId)
    {
        return categoryManagementMapper.selectCategoryManagementByCategoryId(categoryId);
    }

    /**
     * 查询分类管理列表
     * 
     * @param categoryManagement 分类管理
     * @return 分类管理
     */
    @Override
    public List<CategoryManagement> selectCategoryManagementList(CategoryManagement categoryManagement)
    {
        return categoryManagementMapper.selectCategoryManagementList(categoryManagement);
    }

    /**
     * 新增分类管理
     * 
     * @param categoryManagement 分类管理
     * @return 结果
     */
    @Override
    public int insertCategoryManagement(CategoryManagement categoryManagement)
    {
        return categoryManagementMapper.insertCategoryManagement(categoryManagement);
    }

    /**
     * 修改分类管理
     * 
     * @param categoryManagement 分类管理
     * @return 结果
     */
    @Override
    public int updateCategoryManagement(CategoryManagement categoryManagement)
    {
        return categoryManagementMapper.updateCategoryManagement(categoryManagement);
    }

    /**
     * 批量删除分类管理
     * 
     * @param categoryIds 需要删除的分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryManagementByCategoryIds(Long[] categoryIds)
    {
        return categoryManagementMapper.deleteCategoryManagementByCategoryIds(categoryIds);
    }

    /**
     * 删除分类管理信息
     * 
     * @param categoryId 分类管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryManagementByCategoryId(Long categoryId)
    {
        return categoryManagementMapper.deleteCategoryManagementByCategoryId(categoryId);
    }

    @Override
    public List<TreeSelect> selectCategoryTreeList(CategoryManagement categoryManagement) {
        List<CategoryManagement> categoryManagements = categoryManagementMapper.selectCategoryList(categoryManagement);
        return buildCategoryTreeSelect(categoryManagements);
    }


    /**
     * 构建前端所需要下拉树结构
     */
    @Override
    public List<TreeSelect> buildCategoryTreeSelect(List<CategoryManagement> categoryManagements)
    {
        List<CategoryManagement> categoryTree = buildCategoryTree(categoryManagements);
        return categoryTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<CategoryManagement> buildCategoryTree(List<CategoryManagement> categoryManagements) {
        List<CategoryManagement> returnList = new ArrayList<CategoryManagement>();
        List<Long> tempList = categoryManagements.stream().map(CategoryManagement::getCategoryId).collect(Collectors.toList());
        for (CategoryManagement categoryManagement : categoryManagements)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(categoryManagement.getParentId()))
            {
                recursionFn(categoryManagements, categoryManagement);
                returnList.add(categoryManagement);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categoryManagements;
        }
        return returnList;
    }

    @Override
    public boolean hasChildByCategoryId(Long categoryId) {
        int result = categoryManagementMapper.hasChildByCategoryId(categoryId);
        return result > 0;
    }

    @Override
    public boolean checkCategoryUseStatus(Long categoryId) {
        int result = categoryManagementMapper.checkCategoryUseStatus(categoryId);
        return result > 0;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<CategoryManagement> list, CategoryManagement t)
    {
        // 得到子节点列表
        List<CategoryManagement> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CategoryManagement tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<CategoryManagement> getChildList(List<CategoryManagement> list, CategoryManagement t)
    {
        List<CategoryManagement> tlist = new ArrayList<CategoryManagement>();
        for (CategoryManagement n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CategoryManagement> list, CategoryManagement t)
    {
        return getChildList(list, t).size() > 0;
    }
}
